package bugzillapreprocessor.model;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * The issue builder implements the builder pattern for constructing issue
 * objects. The class is also implemented as a singleton.
 * 
 * @author c_haras
 */
public class IssueBuilder {

	private static IssueBuilder instance = null;

	@SuppressWarnings("unused")
	private String issueStatusCode = "";
	private String issueId = "";
	private String issueStatus = "";
	private String priority = "";
	private String resolution = "";
	@SuppressWarnings("unused")
	private String component = "";
	@SuppressWarnings("unused")
	private String version = "";
	@SuppressWarnings("unused")
	private String repPlatform = "";
	private String assignedTo = "";
	private String deltaTs = "";
	@SuppressWarnings("unused")
	private String subcomponent = "";
	private String reporter = "";
	@SuppressWarnings("unused")
	private String targetMilestone = "";
	private String issueType = "";
	private String creationTs = "";
	@SuppressWarnings("unused")
	private String qaContact = "";
	@SuppressWarnings("unused")
	private String statusWhiteboard = "";
	@SuppressWarnings("unused")
	private String issueFileLoc = "";
	@SuppressWarnings("unused")
	private String votes = "";
	@SuppressWarnings("unused")
	private String opSys = "";
	@SuppressWarnings("unused")
	private String shortDescription = "";
	@SuppressWarnings("unused")
	private String keywords = "";
	private ArrayList<String> dependsOnIssueIdList;

	/**
	 * A private constructor
	 */
	private IssueBuilder() {
	}

	/**
	 * Get the instance of the builder
	 * 
	 * @return Instance of the builder
	 */
	public static IssueBuilder getInstance() {
		if (instance == null) {
			instance = new IssueBuilder();
		}
		return instance;
	}

	/**
	 * Build an issue object
	 * 
	 * @return An issue object
	 */
	public Issue build() {
		// It could only be the intersection of used variables, so not all of
		// them
		/*
		 * return new Issue(issueStatusCode, issueId, issueStatus, priority,
		 * resolution, component, version, repPlatform, assignedTo, deltaTs,
		 * subcomponent, reporter, targetMilestone, issueType, creationTs,
		 * qaContact, statusWhiteboard, issueFileLoc, votes, opSys,
		 * shortDescription, keywords, dependsOnIssueId, dependsOnWho,
		 * dependsOnWhen);
		 */
		return new Issue(issueId, issueStatus, priority, resolution,
				assignedTo, deltaTs, reporter, issueType, creationTs,
				dependsOnIssueIdList);
	}

	/**
	 * Initialize a new issue object
	 */
	public void startNewIssue() {
		issueStatusCode = "";
		issueId = "";
		issueStatus = "";
		priority = "";
		resolution = "";
		component = "";
		version = "";
		repPlatform = "";
		assignedTo = "";
		deltaTs = "";
		subcomponent = "";
		reporter = "";
		targetMilestone = "";
		issueType = "";
		creationTs = "";
		qaContact = "";
		statusWhiteboard = "";
		issueFileLoc = "";
		votes = "";
		opSys = "";
		shortDescription = "";
		keywords = "";
		dependsOnIssueIdList = new ArrayList<String>();
	}

	// Getters and setters
	public void withIssueId(String issueId) {
		this.issueId = issueId;
	}

	public void withIssueType(String issueType) {
		this.issueType = issueType;
	}

	public void withIssueStatus(String issueStatus) {
		this.issueStatus = issueStatus;
	}

	public void withResolution(String resolution) {
		this.resolution = resolution;
	}

	public void withDeltaTs(String deltaTs) {
		this.deltaTs = deltaTs;
	}

	public void withCreationTs(String creationTs) {
		this.creationTs = creationTs;
	}

	public void withPriority(String priority) {
		this.priority = priority;
	}

	public void withReporter(String reporter) {
		this.reporter = reporter;
	}

	public void withAssignedTo(String assignedTo) {
		this.assignedTo = assignedTo;
	}

	public void withDependsOnIssueId(String dependsOnIssueId) {
		String[] issueIds = dependsOnIssueId.split("|");
		this.dependsOnIssueIdList.addAll(Arrays.asList(issueIds));
	}
}
